package com.itheima.controller;
import com.itheima.Service.ProductService;
import com.itheima.pojo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/api/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 分页查询商品列表
     */
    @GetMapping
    public Result page(ProductQueryParam productQueryParam) {
        log.info("分页查询商品：{}", productQueryParam);
        PageResult<Product> pageResult = productService.page(productQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 获取商品详情
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("查询商品详情，ID：{}", id);
        Product product = productService.get(id);
        return Result.success(product);
    }

    /**
     * 添加商品
     */
    @PostMapping
    public Result add(@RequestBody Product product) {
        log.info("添加商品：{}", product);
        productService.add(product);
        return Result.success();
    }

    /**
     * 更新商品
     */
    @PutMapping()
    public Result update(@RequestBody Product product) {
        log.info("更新商品信息：{}" ,product);
        productService.update(product);
        return Result.success();
    }

    /**
     * 更新商品状态
     */
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable Integer id, @RequestBody StatusUpdateParam statusUpdateParam) {
        log.info("更新商品状态，ID：{}，状态：{}", id, statusUpdateParam.getStatus());
        productService.updateStatus(id, statusUpdateParam.getStatus());
        return Result.success();
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除商品，ID：{}", id);
        productService.delete(id);
        return Result.success();
    }

}